home *** CD-ROM | disk | FTP | other *** search
- {$DEFINE EXPERT}
- { I changed all NewForm to ResultForm (from the NewF.PAS unit) }
- unit Mainform;
- interface
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, ExtCtrls, Grids, DB, DBTables, Buttons, Mask,
- DBCtrls;
-
- Type
- TForm1 = class(TForm)
- Notebook1: TNotebook;
- Label1: TLabel;
- DatabaseList: TListBox;
- BitBtnNext1: TBitBtn;
- BitBtnNext2: TBitBtn;
- Label2: TLabel;
- TableList: TListBox;
- BitBtnBack2: TBitBtn;
- FieldList: TListBox;
- Label3: TLabel;
- BitBtnNext3: TBitBtn;
- BitBtnBack3: TBitBtn;
- Label4: TLabel;
- BitBtnNext4: TBitBtn;
- BitBtnBack4: TBitBtn;
- FilterGroup: TRadioGroup;
- AllBitBtn: TBitBtn;
- NoneBitBtn: TBitBtn;
- StringGrid1: TStringGrid;
- Table1: TTable;
- procedure Notebook1PageChanged(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure DatabaseListClick(Sender: TObject);
- procedure BitBtnNext1Click(Sender: TObject);
- procedure TableListClick(Sender: TObject);
- procedure BitBtnBack2Click(Sender: TObject);
- procedure BitBtnNext2Click(Sender: TObject);
- procedure BitBtnBack3Click(Sender: TObject);
- procedure AllBitBtnClick(Sender: TObject);
- procedure NoneBitBtnClick(Sender: TObject);
- procedure BitBtnNext3Click(Sender: TObject);
- procedure BitBtnBack4Click(Sender: TObject);
- procedure FieldListClick(Sender: TObject);
- procedure BitBtnNext4Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- {$IFDEF EXPERT}
- UnitName: String;
- {$ENDIF}
- end;
-
- var
- Form1: TForm1;
-
- implementation
- {$R *.DFM}
-
- uses
- Newf;
-
- const FieldTypeCount = 14;
-
- type
- CVTable = array [1..FieldTypeCount, 1..2] of TClass;
-
- const ConvertTable: CVTable = (
- (TStringField, TDBEdit),
- (TIntegerField, TDBEdit),
- (TSmallintField, TDBEdit),
- (TWordField, TDBEdit),
- (TFloatField, TDBEdit),
- (TCurrencyField, TDBEdit),
- (TBCDField, TDBEdit),
- (TBooleanField, TDBCheckBox),
- (TDateTimeField, TDBEdit),
- (TDateField, TDBEdit),
- (TTimeField, TDBEdit),
- (TMemoField, TDBMemo),
- (TBlobField, TDBImage), {just a guess}
- (TGraphicField, TDBImage));
-
- function ConvertClass(FieldClass: TFieldClass) : TControlClass;
- var
- I: Integer;
- Found: Boolean;
- begin
- Found := False;
- for I := 1 to FieldTypeCount do
- if ConvertTable [I, 1] = FieldClass then
- begin
- ConvertClass := TControlClass (ConvertTable [I, 2]);
- Found := True;
- break; {jump out of for loop}
- end;
- if not Found then
- raise Exception.Create ('Match not found');
- end;
-
- procedure NormalizeString (var S: string);
- var
- N: Integer;
- begin
- {remove the T}
- Delete (S, 1, 1);
- {chek if the string is a valid
- Pascal identifier: if not repalce spaces
- and other characters with underscores}
- if not IsValidIdent (S) then
- for N := 1 to Length (S) do
- if not ((S[N] in ['A'..'Z']) or (S[N] in ['a'..'z'])
- or (S[N] in ['0'..'9'])) then
- S [N] := '_';
- end;
-
- procedure TForm1.Notebook1PageChanged(Sender: TObject);
- begin
- {copy the name of the page into the caption}
- Caption := 'Marco & Dr.Bob''s Database Expert - ' +
- NoteBook1.ActivePage;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- {fill the first listbox}
- Session.GetDatabaseNames (
- DatabaseList.Items);
- Notebook1.PageIndex := 0;
- end;
-
- procedure TForm1.DatabaseListClick(Sender: TObject);
- begin
- {once a database is selected, the user
- can move to the following page}
- BitBtnNext1.Enabled := True;
- end;
-
- procedure TForm1.BitBtnNext1Click(Sender: TObject);
- var
- CurrentDB, CurrentFilter: string;
- begin
- CurrentDB := DatabaseList.Items [
- DatabaseList.ItemINdex];
- CurrentFilter := FilterGroup.Items [
- FilterGroup.ItemIndex];
- {get the tables of the current DB}
- Session.GetTableNames (
- CurrentDB, CurrentFilter,
- True, False, TableList.Items);
- {move to the next page}
- NoteBook1.PageIndex := 1;
- BitBtnNext2.Enabled := False;
- end;
-
- procedure TForm1.TableListClick(Sender: TObject);
- begin
- BitBtnNext2.Enabled := True;
- end;
-
- procedure TForm1.BitBtnBack2Click(Sender: TObject);
- begin
- NoteBook1.PageIndex := 0;
- end;
-
- procedure TForm1.BitBtnNext2Click(Sender: TObject);
- var
- I: Integer;
- begin
- {set the properties of a table}
- with Table1 do
- begin
- DatabaseName := DatabaseList.Items[
- DatabaseList.ItemIndex];
- TableName := TableList.Items[
- TableList.ItemIndex];
- {load the fields definition}
- FieldDefs.Update;
- end;
- {clear the list then fill it}
- FieldList.Clear;
- for I := 0 to Table1.FieldDefs.Count - 1 do
- {add number, name, and class name}
- FieldList.Items.Add (Format (
- '%d) %s [%s]',
- [Table1.FieldDefs[I].FieldNo,
- Table1.FieldDefs[I].Name,
- Table1.FieldDefs[I].FieldClass.ClassName]));
- NoteBook1.PageIndex := 2;
- BitBtnNext3.Enabled := False;
- end;
-
- procedure TForm1.BitBtnBack3Click(Sender: TObject);
- begin
- NoteBook1.PageIndex := 1;
- end;
-
- procedure TForm1.AllBitBtnClick(Sender: TObject);
- var
- I: Integer;
- begin
- {select each item}
- for I := 0 to FieldList.Items.Count - 1 do
- FieldList.Selected [I] := True;
- BitBtnNext3.Enabled := True;
- end;
-
- procedure TForm1.NoneBitBtnClick(Sender: TObject);
- var
- I: Integer;
- begin
- {unselect each item}
- for I := 0 to FieldList.Items.Count - 1 do
- FieldList.Selected [I] := False;
- BitBtnNext3.Enabled := False;
- end;
-
- procedure TForm1.BitBtnNext3Click(Sender: TObject);
- var
- I, RowNum: Integer;
- begin
- {reserve enough rows}
- StringGrid1.RowCount := FieldList.Items.Count;
- {empty the string grid}
- for I := 0 to StringGrid1.RowCount - 1 do
- begin
- StringGrid1.Cells [0, I] := '';
- StringGrid1.Cells [1, I] := '';
- end;
- {scan the list of fields, showing the
- corresponding data aware components,
- only if the field is selected}
- RowNum := 0;
- for I := 0 to FieldList.Items.Count - 1 do
- if FieldList.Selected [I] then
- begin
- StringGrid1.Cells [0, RowNum] := Format ('%d) %s [%s]',
- {add number, name, and control class}
- [Table1.FieldDefs[I].FieldNo,
- Table1.FieldDefs[I].Name,
- ConvertClass(Table1.FieldDefs[I].FieldClass).ClassName]);
- StringGrid1.Cells [1, RowNum] := Table1.FieldDefs[I].Name;
- Inc (RowNum);
- end;
- {set the real number of rows}
- StringGrid1.RowCount := RowNum;
- NoteBook1.PageIndex := 3;
- end;
-
- procedure TForm1.BitBtnBack4Click(Sender: TObject);
- begin
- NoteBook1.PageIndex := 2;
- end;
-
- procedure TForm1.FieldListClick(Sender: TObject);
- begin
- BitBtnNext3.Enabled := True;
- end;
-
- {Generate button}
- procedure TForm1.BitBtnNext4Click(Sender: TObject);
- var
- I, RowNum, Y, H, Hmax: Integer;
- NewName: string;
- NewLabel: TLabel;
- NewDBComp: TControl;
- CtrlClass: TControlClass;
- {$IFDEF EXPERT}
- F: System.text;
- {$ENDIF}
- begin
- {Generate the form and connect the table}
- ResultForm := TResultForm.Create (Application);
- with ResultForm.Table1 do
- begin
- DatabaseName := Table1.DatabaseName;
- TableName := Table1.TableName;
- Active := True;
- end;
- {$IFDEF EXPERT}
- {generate the first part of the unit source}
- System.Assign(f,UnitName+'.PAS');
- System.Rewrite(f);
- writeln(f,'unit ',ExtractFileName(UnitName),';');
- writeln(f,'interface');
- writeln(f,'uses');
- writeln(f,' SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,');
- writeln(f,' Forms, Dialogs, DB, DBTables, DBCtrls, ExtCtrls;');
- writeln(f);
- writeln(f,'type');
- writeln(f,' TResultForm = class(TForm)');
- writeln(f,' Panel1: TPanel;');
- writeln(f,' DBNavigator1: TDBNavigator;');
- writeln(f,' ScrollBox1: TScrollBox;');
- writeln(f,' DataSource1: TDataSource;');
- writeln(f,' Table1: TTable;');
- {$ENDIF}
- {generates field editors}
- Y := 10;
- RowNum := 0;
- for I := 0 to FieldList.Items.Count - 1 do
- if FieldList.Selected [I] then
- begin
- {create a label with the field name}
- NewLabel := TLabel.Create (ResultForm);
- NewLabel.Parent := ResultForm.ScrollBox1;
- NewLabel.Name := 'Label' + IntToStr (I);
- {$IFDEF EXPERT}
- writeln(f,' Label',IntToStr(i),': TLabel;');
- {$ENDIF}
- NewLabel.Caption :=
- StringGrid1.Cells [1, RowNum];
- NewLabel.Top := Y;
- NewLabel.Left := 10;
- NewLabel.Width := 130;
-
- {create a control of the proper type,
- using a class reference}
- CtrlClass := ConvertClass (
- Table1.FieldDefs[I].FieldClass);
- NewDBComp := CtrlClass.Create (ResultForm);
- NewDBComp.Parent := ResultForm.ScrollBox1;
- NewName := CtrlClass.ClassName +
- Table1.FieldDefs[I].Name;
- NormalizeString (NewName);
- NewDBComp.Name := NewName;
- {$IFDEF EXPERT}
- writeln(f,' ',NewName,': ',CtrlClass.ClassName,';');
- {$ENDIF}
- NewDBComp.Top := Y;
- NewDBComp.Left := 150;
- NewDbComp.Width := ResultForm.ScrollBox1.Width - 160;
-
- {connect the control with the proper
- data source and field}
- if CtrlClass = TDBEdit then
- begin
- TDBEdit (NewDBComp).DataSource :=
- ResultForm.DataSource1;
- TDBEdit (NewDBComp).DataField :=
- Table1.FieldDefs[I].Name;
- end
- else if CtrlClass = TDBMemo then
- begin
- TDBMemo (NewDBComp).DataSource :=
- ResultForm.DataSource1;
- TDBMemo (NewDBComp).DataField :=
- Table1.FieldDefs[I].Name;
- end
- else if CtrlClass = TDBImage then
- begin
- TDBImage (NewDBComp).DataSource :=
- ResultForm.DataSource1;
- TDBImage (NewDBComp).DataField :=
- Table1.FieldDefs[I].Name;
- TDBImage (NewDBComp).Height :=
- TDBImage (NewDBComp).Height * 2;
- end
- else if CtrlClass = TDBCheckBox then
- begin
- TDBCheckBox (NewDBComp).DataSource :=
- ResultForm.DataSource1;
- TDBCheckBox (NewDBComp).DataField :=
- Table1.FieldDefs[I].Name;
- end;
-
- {compute the position of the next component}
- Y := Y + NewDBComp.Height + 10;
- {increase the string grid row}
- Inc (RowNum);
- end; {end if and for}
-
- {size and show the form}
- H := Y + ResultForm.Panel1.Height;
- HMax := (Screen.Height - 40 -
- (ResultForm.Height - ResultForm.ClientHeight));
- if H > HMax then
- begin
- H := HMax;
- ResultForm.Width := ResultForm.Width +
- GetSystemMetrics (SM_CXVSCROLL);
- end;
- ResultForm.ClientHeight := H;
- {$IFDEF EXPERT}
- writeln(f,' procedure FormClose(Sender: TObject; var Action: TCloseAction);');
- writeln(f,' private');
- writeln(f,' { Private declarations }');
- writeln(f,' public');
- writeln(f,' { Public declarations }');
- writeln(f,' end;');
- writeln(f);
- writeln(f,'var');
- writeln(f,' ResultForm: TResultForm;');
- writeln(f);
- writeln(f,'implementation');
- writeln(f);
- writeln(f,'{$R *.DFM}');
- writeln(f);
- writeln(f,'procedure TResultForm.FormClose(Sender: TObject; var Action: TCloseAction);');
- writeln(f,'begin');
- writeln(f,' Action := caFree;');
- writeln(f,'end;');
- writeln(f);
- writeln(f,'end.');
- System.Close(f);
- ModalResult := mrOk
- {$ELSE}
- ResultForm.Show;
- {$ENDIF}
- end;
-
- end.
-